package com.hzdl.util;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author ：Reus CAI
 * @date ：Created in 2020/12/15 11:35
 * @description：创建JavaBean的工厂
 * @modified By：
 * @version: $
 */


public class BeanFactory {

    /*1.加载配置文件
    2. 通过配置文件提取JavaBean的全限定名*/


    private static InputStream resourceAsStream = null;
    private static Properties properties = null;
    private static Map<String, Object> map = new HashMap<String, Object>();


    static {

        resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("Bean.properties");
        properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Enumeration<String> stringEnumeration = (Enumeration<String>) properties.propertyNames();

        while (stringEnumeration.hasMoreElements()) {
            String key = stringEnumeration.nextElement();
//            System.out.println(key);
            String value = (String) properties.get(key);

            Object valueObj = null;
            try {
                valueObj = Class.forName(value).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            map.put(key, valueObj);
//            System.out.println(map.values());
        }

    }

    public static Object getBean(String beanName) {
        Object obj = map.get(beanName);
        return obj;
    }


    @Test
    public void methodTest01() {

        System.out.println("人生自是有情痴");
        Object accountDaoImpl = BeanFactory.getBean("accountDaoImpl");
        System.out.println(accountDaoImpl);


    }


   /* public static void main(String[] args) {
        System.out.println("人生自是有情痴");
        Object accountDao = BeanFactory.getBean("accountDao");
        System.out.println(accountDao);

    }*/

}
